package tool;


import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.ini4j.Ini;
import org.ini4j.Wini;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientTool {
    /**
     * 发送 get请求
     */
    public static String get(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = null;
        try {
            httpget = new HttpGet(url);
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String body = EntityUtils.toString(entity);
                    return body;
                } else {
//                    LoggerUtils.runtimelogger.warn("get uri entity is empty, uri:" + httpget.getURI() + response.getStatusLine());
                }
            } finally {
                response.close();
            }
        } catch ( ParseException | IOException e) {
//            LoggerUtils.runtimelogger.error("get url error" + httpget.getURI(), e);
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
//                LoggerUtils.runtimelogger.error("close connection error", e);
            }
        }
        return null;
    }

    /**
     * post request
     *
     * @param url
     * @param header
     * @param body
     * @return
     */
    public static String post(String url, Map<String, String> header, String body) {
        HttpPost request = new HttpPost(url);
        return httpRequest(request,body);
    }
    public static String put(String url,String body){
        HttpEntityEnclosingRequestBase request = new HttpPut(url);
        return httpRequest(request,body);
    }
    public static String httpRequest(HttpEntityEnclosingRequestBase request,String body){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        request.setHeader("Accept", "application/json");
        try {
            request.setEntity(new StringEntity(body));
            CloseableHttpResponse response = httpclient.execute(request);
            try {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, "UTF-8");
            } finally {
                response.close();
            }
        } catch (IOException e) {
//            LoggerUtils.runtimelogger.error("http put error",e);
        }finally {
            try {
                httpclient.close();
            } catch (IOException e) {
//                LoggerUtils.runtimelogger.error("http client close error",e);
            }
        }
        return "";
    }

	public static void main(String[] args) throws Exception {
//		List<String> dtas=getHouseId("https://nj.lianjia.com/ershoufang/qiaobei",72)	 ;
//		for(int i=0;i<dtas.size();i++){
//			WriteIni(String.valueOf(i), dtas.get(i));
//		}
//		
//		
//		 System.err.println("完成！" );	
		
		
		String url1=get("https://nj.lianjia.com/ershoufang/103101062477.html");
		 System.err.println(url1 );
	}
     
	
	
	
	public static  List<String> getHouseId(String url,int index){
		List<String> datas= new ArrayList<String>();
		String url1;
		for(int i=0;i<index;i++){
			 
			 if(i==0){
				 url1= url; 
			  }else{
				  url1= url+"/pg"+i+"/";
			  }
			  String hhj=get(url1);
			  String hex="ids:";
			  String hex1= "SIRKeyword:";
			  int he1=  hhj.indexOf(hex);
			  int he2=  hhj.indexOf(hex1);
			  String data=hhj.substring(he1+6,he2-7);
//			  System.out.println(hhj.substring(he1+6,he2-7));
			  String[]  hj=data.split(",");
			  for(String d:hj){
				  datas.add(d); 
				  System.out.println("id:"+d);
			  }
			  try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			  System.out.println("第："+i+"次" );
		  }
		  return datas;
	}
    
    public static void WriteIni(String key,String value) throws IOException {  
        String fileName = "D:\\house.ini";  
        Wini ini = new Wini(new File(fileName));  
//        int age = ini.get("happy", "age", int.class);  
//        double height = ini.get("happy", "height", double.class);  
//        String dir = ini.get("happy", "homeDir");  
//          
//        System.out.println(age);   
//        System.out.println(height);  
//        System.out.println(dir);  
            ini.get("houseid") .add(key, value);
            System.out.println();
        ini.store();
    }  
  
    
}